package com.csu.push;

import com.alibaba.fastjson.JSONObject;
import com.csu.device.entity.Behavior;
import com.csu.device.entity.DeviceRela;
import com.csu.util.DeviceUtil;
import com.csu.util.ThmsWebSocketHandler;
import com.csu.util.ThmsWebSocketMessage;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * webSocket push
 */
public class WebSocketPush {

    /**
     * GPS 推送
     *
     * @param jsonObject
     * @param deviceRelas
     */
    public static void gspPush(JSONObject jsonObject, List<DeviceRela> deviceRelas) {
        //获取websocket
        ThmsWebSocketHandler swsh = ThmsWebSocketHandler.getSystemWebSocketHandler();
        ThmsWebSocketMessage twsm;
        //发送的消息
        try {
            for (DeviceRela deviceRela : deviceRelas) {
                twsm = new ThmsWebSocketMessage("gps", deviceRela.getDeviceBindingId(), "gps" + deviceRela.getDeviceBindingId(), jsonObject);
                swsh.sendMessageToUserFamily(twsm);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 体征检测推送
     *
     * @param jsonObject
     * @param deviceRelas
     */
    public static void measurePush(JSONObject jsonObject, List<DeviceRela> deviceRelas, String userId) {
        //获取websocket
        ThmsWebSocketHandler swsh = ThmsWebSocketHandler.getSystemWebSocketHandler();
        ThmsWebSocketMessage twsm;

        //单独推送下老人信息
        if (null != userId) {
            twsm = new ThmsWebSocketMessage("measure", userId, "measure" + userId, jsonObject);
            swsh.sendMessageToUsers(twsm);
        } else {
            //发送的消息
            try {
                for (DeviceRela deviceRela : deviceRelas) {
                    twsm = new ThmsWebSocketMessage("measure", deviceRela.getDeviceBindingId(), "measure" + deviceRela.getDeviceBindingId(), jsonObject);
                    swsh.sendMessageToUserFamily(twsm);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }


    /**
     * ifid 开卡推送
     * @param deviceSn
     * @param gatewayType
     */
    public static void ifidPush(String deviceSn,String gatewayType) {
        //获取websocket
        ThmsWebSocketHandler swsh = ThmsWebSocketHandler.getSystemWebSocketHandler();
        ThmsWebSocketMessage twsm = new ThmsWebSocketMessage(gatewayType, "10009", "RFID1009"  , deviceSn);
        swsh.sendMessageToUserFamily(twsm);
    }


    /**
     * ifid 开卡推送
     * @param deviceSn
     * @param gatewayType
     */
    public static void ifidPush2(String gatewayType,JSONObject jsonObject) {
        //获取websocket
        ThmsWebSocketHandler swsh = ThmsWebSocketHandler.getSystemWebSocketHandler();
        ThmsWebSocketMessage twsm = new ThmsWebSocketMessage(gatewayType, "10009", "RFID10089"  , jsonObject);
        swsh.sendMessageToUserFamily(twsm);
    }


    /**
     * 小米设备推送
     * @param jsonObject
     * @param deviceRelas
     */
    public static void xiaoMiPush(JSONObject jsonObject, List<DeviceRela> deviceRelas,String deviceSn,Integer deviceType) {
        //获取websocket
        ThmsWebSocketHandler swsh = ThmsWebSocketHandler.getSystemWebSocketHandler();
        ThmsWebSocketMessage twsm;
        //发送的消息
        try {
            for (DeviceRela deviceRela : deviceRelas) {
                twsm = new ThmsWebSocketMessage(deviceSn, deviceType+"",  deviceRela.getDeviceBindingId(), jsonObject);
                swsh.sendMessageToUserFamily(twsm);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 设备异常推送
     * @param behavior
     */
    public static void errorPush(Behavior behavior) {
        //获取websocket
        ThmsWebSocketHandler swsh = ThmsWebSocketHandler.getSystemWebSocketHandler();
        ThmsWebSocketMessage twsm;
        //发送的消息
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

            behavior.setvTime(sdf.format(behavior.getStartTime()));
            twsm = new ThmsWebSocketMessage("error", null, "error",  "");
            swsh.sendMessageToUserFamily(twsm);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 推送床垫的websocket信息
     * @param deviceRelas
     * @param jsonObject
     */
    public static void mattressPush(List<DeviceRela> deviceRelas, JSONObject jsonObject) {

        try{
            jsonObject.put("timeCreate",DeviceUtil.formatCreateDate(jsonObject.getString("timeCreate")));
        }catch (Exception e){

        }


        ThmsWebSocketHandler swsh = ThmsWebSocketHandler.getSystemWebSocketHandler();
        ThmsWebSocketMessage twsm;
        for (DeviceRela deviceRela : deviceRelas) {
            twsm = new ThmsWebSocketMessage("Mattress", deviceRela.getDeviceBindingId(), deviceRela.getDeviceBindingId(), jsonObject);
            //发送
            swsh.sendMessageToUserFamily(twsm);
        }
    }


    /**
     * 推送所有信息
     * @param jsonObject
     */
    public static void sendAllMessage(JSONObject jsonObject,String deviceSn) {

        try{
            jsonObject.put("timeCreate",DeviceUtil.formatCreateDate(jsonObject.getString("timeCreate")));
        }catch (Exception e){

        }
        ThmsWebSocketHandler swsh = ThmsWebSocketHandler.getSystemWebSocketHandler();
        ThmsWebSocketMessage twsm = new ThmsWebSocketMessage(deviceSn, "100861", "100861", jsonObject);
        //发送
        swsh.sendMessageToUserFamily(twsm);
    }

    public static  void sendAppUserMessageByMeasure(Behavior behavior){
        //获取websocket
        ThmsWebSocketHandler swsh = ThmsWebSocketHandler.getSystemWebSocketHandler();
        ThmsWebSocketMessage twsm;
        //发送的消息
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

            behavior.setvTime(sdf.format(behavior.getStartTime()));
            twsm = new ThmsWebSocketMessage(behavior.getApplicationId(), behavior.getFamilyUserId(), behavior.getFamilyUserId(),  new Gson().toJson(behavior));
            swsh.sendMessageToUserFamily(twsm);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
